﻿using FTBS.Models.BLO;
using FTBS.Models.Entities;
using FTBS.Models.Entities.Concert;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PagedList;

namespace FTBS.Controllers
{
    public class TicketCartController : Controller
    {
        private TicketCartBLO _ticketCartBLO = new TicketCartBLO();

        public ActionResult Index(string sortOrder, string currentFilter, string searchString, int? page, bool? isSortDescending)
        {
            List<TicketCartConcertEntity> listTicketCart = new List<TicketCartConcertEntity>();
            listTicketCart = _ticketCartBLO.GetAllTicketCart();

            ViewBag.NameSortParm = "cartId_desc";
            ViewBag.CurrentSort = sortOrder;

            if (searchString != null)
            {
                page = 1;
            }
            else
            {
                searchString = currentFilter;
            }
            ViewBag.CurrentFilter = searchString;
            ViewBag.IsSortDes = false;

            if (!String.IsNullOrEmpty(searchString))
            {
                listTicketCart = listTicketCart.Where(ticketCart => ticketCart.CartId.ToUpper().Contains(searchString.ToUpper())
                                                || ticketCart.UserName.ToUpper().Contains(searchString.ToUpper())
                                                || ticketCart.TotalPrice.ToString().ToUpper().Contains(searchString.ToUpper())
                                                || ticketCart.TotalTicket.ToString().ToUpper().Contains(searchString.ToUpper())
                                                || ticketCart.StatusPayment.ToUpper().Contains(searchString.ToUpper())
                                                || ticketCart.Note.ToUpper().Contains(searchString.ToUpper())
                                                || ticketCart.CreateTime.ToShortDateString().ToUpper().Contains(searchString.ToUpper())).ToList();
            }

            switch (sortOrder)
            {
                case "cartId_desc":
                    if (isSortDescending == true)
                    {
                        listTicketCart = listTicketCart.OrderByDescending(ticketCart => ticketCart.CartId).ToList();
                        ViewBag.IsSortDes = false;
                    }
                    else
                    {
                        listTicketCart = listTicketCart.OrderBy(ticketCart => ticketCart.CartId).ToList();
                        ViewBag.IsSortDes = true;
                    }
                    break;
                case "user_desc":
                    if (isSortDescending == true)
                    {
                        listTicketCart = listTicketCart.OrderByDescending(ticketCart => ticketCart.UserName).ToList();
                        ViewBag.IsSortDes = false;
                    }
                    else
                    {
                        listTicketCart = listTicketCart.OrderBy(ticketCart => ticketCart.UserName).ToList();
                        ViewBag.IsSortDes = true;
                    }
                    break;
                case "price_desc":
                    if (isSortDescending == true)
                    {
                        listTicketCart = listTicketCart.OrderByDescending(ticketCart => ticketCart.TotalPrice).ToList();
                        ViewBag.IsSortDes = false;
                    }
                    else
                    {
                        listTicketCart = listTicketCart.OrderBy(ticketCart => ticketCart.TotalPrice).ToList();
                        ViewBag.IsSortDes = true;
                    }
                    break;
                case "totalTicket_desc":
                    if (isSortDescending == true)
                    {
                        listTicketCart = listTicketCart.OrderByDescending(ticketCart => ticketCart.TotalTicket).ToList();
                        ViewBag.IsSortDes = false;
                    }
                    else
                    {
                        listTicketCart = listTicketCart.OrderBy(ticketCart => ticketCart.TotalTicket).ToList();
                        ViewBag.IsSortDes = true;
                    }
                    break;
                case "status_desc":
                    if (isSortDescending == true)
                    {
                        listTicketCart = listTicketCart.OrderByDescending(ticketCart => ticketCart.StatusPayment).ToList();
                        ViewBag.IsSortDes = false;
                    }
                    else
                    {
                        listTicketCart = listTicketCart.OrderBy(ticketCart => ticketCart.StatusPayment).ToList();
                        ViewBag.IsSortDes = true;
                    }
                    break;
                case "note_desc":
                    if (isSortDescending == true)
                    {
                        listTicketCart = listTicketCart.OrderByDescending(ticketCart => ticketCart.Note).ToList();
                        ViewBag.IsSortDes = false;
                    }
                    else
                    {
                        listTicketCart = listTicketCart.OrderBy(ticketCart => ticketCart.Note).ToList();
                        ViewBag.IsSortDes = true;
                    }
                    break;
                case "dateCreate_desc":
                    if (isSortDescending == true)
                    {
                        listTicketCart = listTicketCart.OrderByDescending(ticketCart => ticketCart.CreateTime).ToList();
                        ViewBag.IsSortDes = false;
                    }
                    else
                    {
                        listTicketCart = listTicketCart.OrderBy(ticketCart => ticketCart.CreateTime).ToList();
                        ViewBag.IsSortDes = true;
                    }
                    break;
                default:
                    listTicketCart = listTicketCart.OrderByDescending(ticketCart => ticketCart.CreateTime).ToList();
                    ViewBag.IsSortDes = false;
                    break;
            }

            int pageSize = 6;
            int pageNumber = (page ?? 1);

            return View(listTicketCart.ToPagedList(pageNumber, pageSize));
        }

        public ActionResult PaymentDetail(string id)
        {
            PaymentConcertEntity payment = new PaymentConcertEntity();
            payment = _ticketCartBLO.GetPaymentByCartId(id);
            //ViewBag.TicketCartId = id;
            return View(payment);
        }

        public ActionResult Create(string id)
        {
            PaymentConcertEntity payment = new PaymentConcertEntity();
            payment = _ticketCartBLO.GetPaymentByCartId(id);
            //TicketCart ticketCart = new TicketCart();
            //ticketCart = _ticketCartBO.GetTicketCartByCartId(id);
            ////ViewBag.TicketCartInfor = ticketCart;
            //if (payment.PaymentId == 0)
            //{
            //    payment.CartId = ticketCart.CartId;
            //    payment.ChargeMoney = ticketCart.TotalPrice;
            //    payment.IsComplete = false;
            //}
            return View(payment);
        }

        [HttpPost]
        public ActionResult Create(Payment payment)
        {
            if (ModelState.IsValid)
            {
                _ticketCartBLO.CreatePayment(payment);
                payment = _ticketCartBLO.GetPaymentByCartId(payment.CartId);
                return View("PaymentDetail", payment);
            }
            return View(payment);
        }

        public ActionResult PaymentInfo(string id)
        {
            PaymentConcertEntity payment = new PaymentConcertEntity();
            payment = _ticketCartBLO.GetPaymentByCartId(id);
            TicketCart ticketCart = new TicketCart();
            ticketCart = _ticketCartBLO.GetTicketCartByCartId(id);
            //ViewBag.TicketCartInfor = ticketCart;
            if (payment.PaymentId == 0)
            {
                payment.CartId = ticketCart.CartId;
                payment.ChargeMoney = ticketCart.TotalPrice;
                payment.IsComplete = false;
            }
            return View(payment);
        }

        [HttpPost]
        public ActionResult PaymentInfo(PaymentConcertEntity payment)
        {
            if (ModelState.IsValid)
            {
                string confirmPerson = this.Session["UserName"] as string;
                payment.ConfirmPerson = confirmPerson;
                int resultUpdate = _ticketCartBLO.UpdatePayment(payment);
                if (resultUpdate == 1)
                {

                    payment = _ticketCartBLO.GetPaymentByCartId(payment.CartId);
                    return View("PaymentDetail", payment);
                }
                else
                {
                    return View("PaymentDetail", payment);
                }
            }
            return View(payment);
        }

        /// <summary>
        /// Delete a payment
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <author>hungph</author>
        /// <date>3/3/2014</date>
        [HttpDelete, ActionName("Delete")]
        public ActionResult Delete(int id = 0)
        {
            Payment payment = _ticketCartBLO.GetPaymentById(id);
            if (payment == null)
            {
                return HttpNotFound();
            }
            _ticketCartBLO.DeletePayment(payment);
            return RedirectToAction("Index");
        }
    }
}
